<?php

/***
 * '*' -- form ruler
 * (post|topic|forum).(edit|delete).{uname}' -- personal controls 
 * user.(edit|delete).{uname}
 * 
 *
 **/

// string -> string -> bool
function hasPerm( $perm, $permList=null, $uid=null ) {
  if( !isset($permList) ) {
    if( !isset($_SESSION['uid']) ) {
      loginGuest();
    }
    $permList = $_SESSION['user']['perm'];
  }

  if( $permList == '*' ) 
    return true;
  if( $_SESSION['uid'] > 0 && $uid == $_SESSION['uid'] )
    return true;

  // if you can write me a clean regex to do the same, please do Michael
  $permArr =  explode('.', $perm);

  $skey = array_shift($permArr);
  for( $i = count($permArr); $i > 0; --$i) {
    $reg .= $skey . '(?!\.)|';
    $skey .= '.' . array_shift($permArr);
  }
  $reg = '/'. $reg . $skey . '(?!\.)' . '/';

  return (bool)preg_match($reg, $permList);
}

// set guest login stats
function loginGuest() {
  require_once 'db/get/user.php';
  $user = loginUser('guest','');

  // XXX On false login, will user be null or will it be array()?
  if( isset($user['id']) ) {  
    $_SESSION['uid'] = $user['id'];
    unset($_SESSION['user']);
    $_SESSION['user']['name'] = $user['name'];
    $_SESSION['user']['perm'] = $user['perm'];
    $user = null;
  } else {
    return -1;
  }
}

?>
